import json

from django.http import JsonResponse
from django.conf import settings
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer


class BookViewSet(viewsets.ModelViewSet):
    """处理图书的完整CRUD操作"""

    queryset = Book.objects.all()
    serializer_class = BookSerializer


def get_book(request):
    print("获取书本内容")
    resp = JsonResponse({"name": "Python", "price": 100})
    resp["Access-Control-Allow-Origin"] = "*"
    return resp


def get_films(request):
    with open(
        settings.BASE_DIR.joinpath("data", "films", "films.json"), "r", encoding="utf-8"
    ) as f:
        films_json = json.load(f)
    resp = JsonResponse(films_json)
    resp["Access-Control-Allow-Origin"] = "*"
    return resp
